<template>
  <view class="new-or-edit-modal">
    <up-popup v-model:show="show" overlay mode="bottom" touchable maxHeight="'80%'" closeable closeOnClickOverlay
      @close="close" @open="open">
      <view class="modal-content">
        <view class="modal-header">
          <view class="modal-title">处理任务</view>
        </view>
        <!-- uview-plus的表单，包含标题，公告内容，单选是否弹窗推送，状态下拉，确认按钮 -->
        <up-form labelPosition="left" :model="formModel" :rules="rules" ref="formRef">
          <up-form-item labelWidth="100" label="处理状态" prop="isTrue">
            <up-radio-group v-model="formModel.isTrue">
              <up-radio label="未处理" name="0"></up-radio>
              <up-radio label="已处理" name="1"></up-radio>
              <up-radio label="驳回" name="2"></up-radio>
            </up-radio-group>
          </up-form-item>
          <!-- 泊位状态 -->
          <up-form-item labelWidth="100" label="泊位状态" prop="berthStatus">
            <up-radio-group v-model="formModel.berthStatus">
              <up-radio label="正常" name="0"></up-radio>
              <up-radio label="异常" name="1"></up-radio>
            </up-radio-group>
          </up-form-item>
          <up-form-item labelWidth="100" label="备注" prop="remark">
            <up-input v-model="formModel.remark" placeholder="请输入备注" />
          </up-form-item>
          <up-form-item labelWidth="100" label="上传图片" prop="images">
            <up-upload :fileList="formModel.images" :maxCount="3" :maxSize="1024 * 1024 * 10" accept="image/*" />
          </up-form-item>
        </up-form>
      </view>

      <view class="modal-footer">
        <up-button type="primary" @click="submit">确认</up-button>
      </view>
    </up-popup>
  </view>
</template>

<script lang="ts" setup>
import { ref, reactive } from 'vue';
import { dealNetworkErrorJob } from '@/api';

const formRef = ref(null);
defineOptions({ name: "CheckModal" });

// 创建响应式数据  
const show = defineModel<boolean>("show", { default: false });
const props = defineProps({
  rowData: {
    type: Object,
    default: () => ({}),
  },
});

const emit = defineEmits(["confirm", "cancel"]);
const loading = ref(false);
const formModel = ref(createDefaultModel());
/**
 * 数据模型
 * 是否属实单选框
 * 备注文本框
 * 上传图片
 */
function createDefaultModel() {
  return {
    isTrue: '',
    berthStatus: '',
    remark: '',
    images: [],
  };
}
const rules = reactive({
  isTrue: [{ required: true, message: '请选择是否属实', trigger: 'blur' }],
  berthStatus: [{ required: true, message: '请选择泊位状态', trigger: 'blur' }],
  remark: [{ required: true, message: '请输入备注', trigger: 'blur' }],
})

const submit = async () => {
  try {
    await formRef.value.validate();
    loading.value = true;
    
    // 准备请求参数
    const params = {
      bizCode: props.rowData.bizCode,
      dealRemark: formModel.value.remark,
      fileUrl: formModel.value.images.join(','),
      dealStatus: formModel.value.isTrue === '1' ? '0' : '1' // 0.已处理 1.无法处理
    };
    
    const res = await dealNetworkErrorJob(params);
    if (res.success) {
      uni.showToast({
        title: "处理成功",
        icon: "success",
      });
      emit("confirm");
      show.value = false;
    } else {
      uni.showToast({
        title: res.msg || "处理失败",
        icon: "error",
      });
    }
  } catch (error) {
    console.log(error);
    uni.showToast({
      title: "表单验证失败",
      icon: "none",
    });
  } finally {
    loading.value = false;
  }
}

// 定义方法  
function open() {
  show.value = true;
}

function close() {
  // 关闭逻辑，设置 show 为 false  
  show.value = false;
}  
</script>

<style scoped lang="scss">
.new-or-edit-modal {
  .modal-header {
    text-align: center;
    font-size: 32rpx;
    font-weight: bold;
    color: #333;
  }

  .modal-content {
    padding: 20rpx;
  }
}
</style>
